﻿@model IEnumerable<EC_ASS.Models.Category>

@{
    var product = ViewBag.SelectedItem;
}

@if (!(String.IsNullOrEmpty(ViewBag.EMS)))
{
    <div class="alert alert-success">
        @ViewBag.EMS
    </div>
}
else
{
    <form action="@Url.Action("EditItem", "Admin")" class="form-horizontal" style="margin-top: 20px;" method="post" id="editForm">
        <div class="control-group">
            <label class="control-label" for="inputName">Tên</label>
            <div class="controls">
                <input value="@product.Name" type="text" id="inputName" name="Name" readonly="readonly"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputNumber">Số lượng</label>
            <div class="controls">
                <input value="@product.NumOfProduct" type="number" id="inputNumber" name="NumOfProduct" required />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputSource">Nguồn gốc nhập hàng</label>
            <div class="controls">
                <input value="@product.Source" type="text" id="inputSource" name="Source" required />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputCategory">Danh mục</label>
            <div class="controls">
                <select name="CatID">
                    @foreach (var item in Model)
                    {
                        if (product.CatID == item.ID)
                        {
                        <option value="@item.ID" selected>@item.Name</option>
                        }
                        else
                        {
                        <option value="@item.ID">@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputSize">Size</label>
            <div class="controls">
                <input value="@product.Size" type="text" id="inputSize" name="Size" required />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPrice">Price</label>
            <div class="controls">
                <input value="@product.Price" type="number" id="inputPrice" name="Price" required />
            </div>
        </div>
        <input type="hidden" name="ID" value="@product.ID"/>
        <div class="control-group">
            <div class="controls">
                <button type="submit" class="btn">Cập nhập</button>
            </div>
        </div>
    </form>
}

<!--Submit edit form-->
<script>
    $(function () {
        $('#editForm').submit(function (e) {
            e.preventDefault();
            $('#editLoading').show();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#editLoading').hide();
                    $('#editModalContent').html(result);
                    setTimeout(function () {
                        $('#editModal').modal('hide');
                        //// Refresh:
                        $('#itemTable').load('@Url.Action("_ListItem", "Admin")').hide().fadeIn('slow');
                    }, 2000);
                }
            });
            return false;
        });
    });
</script>
